﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
//using System.Web.Mail;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.Net.Mail;

public partial class GUI_ForgotPassword : System.Web.UI.Page
{
    string username;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string sql = @"SELECT U_username FROM [User] WHERE U_Email='" + txtEmail.Text.ToString() + "'";
            SqlConnection conn = ToolsDT.getConnect();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                username = reader.GetString(0);
            }
            cmd.Dispose();
            conn.Close();
        }
        

    }

    public string CreateLostPassword(int PasswordLength)
    {
        string _allowedChars = "abcdefghijk0123456789mnopqrstuvwxyz";
        Random randNum = new Random();
        char[] chars = new char[PasswordLength];
        int allowedCharCount = _allowedChars.Length;

        for (int i = 0; i < PasswordLength; i++)
        {
            chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
        }
        return new string(chars);
    }

    private string ContentMail()
    {
        string contents = "";

        string newpass = "", Username = "";
        if (username != null)
        {
            contents = "This is automatic email send from website OnlineSMS...";
            newpass = CreateLostPassword(6);
            contents += "Your new password is: " + newpass;
            Username = username;
            ToolsDT.UpdatePassword(username, newpass);
        }

        return contents;
    }
    protected void btnRecover_Click(object sender, EventArgs e)
    {
        if (username != null)
        {
            string email = txtEmail.Text;
            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.Credentials = new System.Net.NetworkCredential("sontunga1@gmail.com", "tungminh2007");
            SmtpServer.Port = 587;
            SmtpServer.Host = "smtp.gmail.com";
            SmtpServer.EnableSsl = true;
            MailMessage mail = new MailMessage();
            String[] addr = (email).Split(',');
            try
            {
                mail.From = new MailAddress("admin@onlinesms.com",
                "Online SMS", System.Text.Encoding.UTF8);
                Byte i;
                for (i = 0; i < addr.Length; i++)
                    mail.To.Add(addr[i]);
                mail.Subject = "Send  Recover Password Mail";
                mail.Body = ContentMail();
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.ReplyTo = new MailAddress(email);
                SmtpServer.Send(mail);
                Response.Write(@"<script language='javascript'>alert('Send recover password mail successful ! Please check your email')</script>");
            }
            catch (Exception ex) 
            {
                Console.WriteLine("Error: " + ex);
            }
        }
        else
           
        Response.Write(@"<script language='javascript'>alert('Email not registed !')</script>");
  
    }
    
}
